package com.abl.service.system.impl;

import com.abl.common.mybatis.dao.Dao;
import com.abl.common.mybatis.model.Dnt;
import com.abl.common.mybatis.strengthen.Wrapper;
import com.abl.domain.model.AblError;
import com.abl.domain.model.Param;
import com.abl.common.util.StringKit;
import com.abl.common.util.coll.ArrayKit;
import com.abl.service.system.SysOrgService;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 资源表实现
 */
@Slf4j
@Service
public class SysOrgServiceImpl implements SysOrgService {
    @Autowired
    private Dao dao;

    //新增
    @Override
    public long add(Param<JSONObject> param) {
        //校验重复
        String name = param.getString("name");
        String pid = param.getString("pid");
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq("pid", pid);
        qw.eq("name", name);
        long sys_org = dao.count("sys_org", qw);
        if (sys_org > 0) throw new AblError("操作失败：【部门名称:{0}】已经存在", name);

        Dnt dnt = Dnt.createInsert(param);
        //遍历树结构
        JSONArray objects = getTreeIds(dnt.getString("id"), dnt.getString("pid"));
        dnt.set("ids", objects.toString());

        return dao.insert("sys_org", dnt);
    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean del(Param<JSONObject> param) {
        ArrayList<Object> ids = param.getList("ids");

        //汇总待删除IDS(只要ids字段包含【"id"】)
        HashSet<Object> delIds = new HashSet<>();
        for (Object id : ids) {
            Wrapper<Object> wrapper = new Wrapper<>();
            wrapper.select("id");
            wrapper.eq("d_s", 0);
            wrapper.like("ids", "\"" + id + "\"");
            delIds.addAll(dao.listObj("sys_org", wrapper, String.class));
        }

        //逻辑删除
        if (ArrayKit.isNotBlank(delIds)) {
            Wrapper<Object> uw = new Wrapper<>();
            uw.in("id", delIds);
            uw.set("d_s", 1);
            long sys_menu = dao.update("sys_org", uw);
        }

        return true;
    }

    //修改
    @Override
    public long edit(Param<JSONObject> param) {
        //校验重复
        String id = param.getString("id");
        String name = param.getString("name");
        String pid = param.getString("pid");
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq("name", name);
        qw.eq("pid", pid);
        qw.ne("id", id);
        long sys_org = dao.count("sys_org", qw);
        if (sys_org > 0) throw new AblError("操作失败：【部门名称:{0}】已经存在", name);

        Dnt dnt = Dnt.createUpdate(param);

        //设置数据结构数据

        JSONArray objects = getTreeIds(id, pid);
        dnt.set("ids", objects.toString());

        return dao.updateById("sys_org", dnt);
    }

    private JSONArray getTreeIds(String id, String pid) {
        Wrapper<Object> qw;
        JSONArray objects = new JSONArray();
        objects.add(id);
        if (!"0".equals(pid)) {
            objects.add(0, id);
            Object PID;
            do {
                qw = new Wrapper<>();
                qw.eq("id", id);
                PID = dao.selectObjOrigin("select pid from sys_org", qw);
                if (PID != null && !PID.toString().equals("0")) {
                    objects.add(0, PID.toString());
                    id = PID.toString();
                }
            } while (PID != null && !PID.toString().equals("0"));
        }
        return objects;
    }

    //查询详情
    @Override
    public Dnt info(Param<JSONObject> param) {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.eq("id", param.getOrDefault("id", 0));
        return dao.selectOne("sys_org", wrapper);
    }

    //查询集合
    @Override
    public List<Dnt> list(Param<JSONObject> param) {
        String name = param.getString("name");
        String rpb_user_name = param.getString("rpb_user_name");
        String rpb_user_phone = param.getString("rpb_user_phone");
        String status = param.getString("status");

        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.eq(StringKit.isNotBlank(param.getString("id")), "id", param.getString("id"));
        qw.eq(StringKit.isNotBlank(status), "status", status);

        qw.and(StringKit.isNotBlank(name), e -> {
            e.like("name", name);
            e.or();
            e.like("pname", name);
        });

        qw.and(StringKit.isNotBlank(rpb_user_name), e -> {
            e.like("first_rpb_user_name", rpb_user_name);
            e.or();
            e.like("second_rpb_user_name", rpb_user_name);
        });

        qw.and(StringKit.isNotBlank(rpb_user_phone), e -> {
            e.like("first_rpb_user_phone", rpb_user_phone);
            e.or();
            e.like("second_rpb_user_phone", rpb_user_phone);
        });

        qw.orderByAsc("sort");

        if (param.isPage()) {
            return dao.page("sys_org", qw, param.pageCurrent(), param.pageSize());
        } else {
            return dao.list("sys_org", qw);
        }
    }

    //模糊查询条件下拉列表
    @Override
    public List<Dnt> searchInput(Param<JSONObject> param) {
        String sys_name = param.getString("name", "");
        String sys_path = param.getString("path", "");

        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.last(" limit 30 ");

        if (StringKit.isNotBlank(sys_name)) {
            qw.select("name");
            qw.like("name", sys_name);
            qw.groupBy("name");
            return dao.list("sys_org", qw);
        }

        if (StringKit.isNotBlank(sys_path)) {
            qw.select("path");
            qw.like("path", sys_path);
            qw.groupBy("path");
            return dao.list("sys_org", qw);
        }

        return new ArrayList<>();
    }

    //    模糊查询条件下拉用户集合
    @Override
    public List<Dnt> searchUser(Param<JSONObject> param) {
        String name = param.getString("val", "");

        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", 0);
        qw.select("id,name");
        qw.like("name", name);
        qw.orderByAsc("sort");
        qw.last(" limit 30 ");

        return dao.list("sys_user_prop", qw);
    }


}
